<template>
  <resize-drawer
    title="导入SIP号"
    @onCloseDraw="closeDraw"
    @onCancelFun="cancelFun"
    @onSave="onSave"
    :isShowMoreBtn="true"
    :drawer="drawerShow"
    :size="576"
    class="addForm"
  >
    <template #column>
      <div class="importTop">
        <div class="btns">
          <el-button size="mini" class="fl importBtn">
            <a href="/temp/sip-Import.xlsx" download="批量导入Sip号">下载模板</a>
          </el-button>
          <el-upload
            class="upload-demo"
            ref="importSip"
            accept=".xls, .xlsx"
            :action="importUrl"
            :limit="1"
            :on-success="handleImportSuccess"
            :before-upload="beforeUpload"
            :on-remove="handleRemove"
          >
            <el-button type="primary" size="mini" class="uploadBtn" style="margin-left: 10px">上传文件</el-button>
          </el-upload>
        </div>
        <p class="uploadTip">请下载模板并按照模板格式填写数据后再进行导入，<br />单次带入只读取第一个sheet中的数据。</p>
      </div>
      <div class="content">
        <p><span class="MC_iconfont MC_icon-xinxi-mian"></span>小贴士</p>
        <ul>
          <li v-for="(item, index) in importTitle" :key="index">{{ item }}</li>
        </ul>
      </div>
    </template>
  </resize-drawer>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from 'vue'
import resizeDrawer from '@/common/components/resize-drawer/index.vue'
import { ElMessage } from 'element-plus'

export default defineComponent({
  name: 'importSip',
  components: {
    resizeDrawer
  },
  setup() {
    const state = reactive({
      importTitle: [
        '1.请勿变更表头，*号项为必填项;',
        '2.SIP号：3-6位纯数字；',
        '3.密码：6-18位的数字、大小写字母；',
        '4.是否NAT：是/否（不填写默认为否）；',
        '5.语音编码：G711A/G711U/GSM/G729/ILBC/VP8(不填写默认为G711U)；',
        '6.单次导入上限10000条。'
      ],
      filepath: '',
      drawerShow: false
    })

    function beforeUpload(file: any) {
      // 上传前验证文件合法性
      const fileName = /[^.]+$/.exec(file.name.replace(/\s/g, ''))
      if (fileName && fileName[0] !== 'xls' && fileName[0] !== 'xlsx') {
        ElMessage.error({ message: '仅支持上传xls,xlsx文件', type: 'error' })
        return false
      }
    }

    function uploadError() {
      // 上传失败
      ElMessage.error({ message: '文件上传失败，请重新选择文件上传', type: 'error' })
    }

    function handleImportSuccess(res: any) {
      // 上传成功
      if (res && res.success && res.data) {
        state.filepath = res.data.path
      }
    }

    function handleRemove() {
      state.filepath = ''
    }

    function openOrCloseDrawer(data: any) {
      // 打开或者是关闭抽屉 || 父级调用
      state.drawerShow = !state.drawerShow
      state.filepath = ''
    }

    function importSaveFun() {
      // SIP号导入保存事件
      if (!state.filepath) {
        ElMessage.error('请选择文件上传')
        return
      }
    }

    // 关闭抽屉
    const closeDraw = () => {
      state.drawerShow = false
    }

    // 关闭抽屉
    const cancelFun = () => {
      state.drawerShow = false
    }
    return {
      ...toRefs(state),
      uploadError,
      beforeUpload,
      handleImportSuccess,
      handleRemove,
      openOrCloseDrawer,
      importSaveFun,
      closeDraw,
      cancelFun
    }
  }
})
</script>

<style lang="stylus" scoped>
.importTop
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 24px 16px;
    gap: 16px;
    width: 528px;
    height: 130px;
    background: var(--mc-gray2-color);
    border: 1px dashed var(--mc-gray4-color);
    border-radius: 4px;
    flex: none;
    order: 0;
    align-self: stretch;
    flex-grow: 0;
    .btns
        display: flex;
        flex-direction: row;
        align-items: flex-start;
        padding: 0px;
        gap: 8px;
        width: 170px;
        height: 30px;
        flex: none;
        order: 0;
        flex-grow: 0;
        margin-top 24px
    .uploadTip
        font-weight: 400;
        font-size: 12px;
        line-height: 18px;
        text-align: center;
        letter-spacing: 0.001em;
        color: rgba(0, 0, 0, 0.38)
        flex: none;
        order: 1;
        flex-grow: 0;
        margin 0
    .importBtn,.uploadBtn
            font-size 12px
        a
            color: rgba(0, 0, 0, 0.88);
            text-decoration-line: none;
    .importBtn:hover a
        color $c-main
.MC_icon-xinxi-mian
    color var(--mc-warning-color)
    margin-right 10px
.content
  margin-top 26px
  p
    text-align left
    font-size: 14px;
    line-height: 20px;
    color: rgba(0, 0, 0, 0.88);
  ul
    padding 0
    li
        list-style none
        color: rgba(0, 0, 0, 0.6);
        line-height 18px
        font-size 12px
</style>
